﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using WeShare.WeiXin.Common;
using WeShare.WeiXin.Mp.Card.Entity;
using WeShare.WeiXin.Mp.Poi.Entity;

namespace WeShare.WeiXin.Mp.Poi
{
    public class PoiApi
    {
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="filename">文件的路径，支持网络图片</param>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static ImgUrl UploadImg(string filename, string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={OpenApi.GetToken(tokenOrAppId)}";
            var form = new List<FormEntity>
            {
                new FormEntity { IsFile = true, Name = "buffer", Value = filename }
            };
            return HttpUtil.UploadResult<ImgUrl>(url, form);
        }
        /// <summary>
        /// 添加门店
        /// </summary>
        /// <param name="info"></param>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static BaseRes Add(PoiInfo info, string tokenOrAppId = "")
        {
            var data = new BusinessInfo { business = new BusinessInfo.BaseInfo { base_info = info } };
            var url = $"http://api.weixin.qq.com/cgi-bin/poi/addpoi?access_token={OpenApi.GetToken(tokenOrAppId)}";
            return HttpUtil.PostResult<BaseRes>(data, url);
        }
        /// <summary>
        /// 获取门店信息
        /// </summary>
        /// <param name="poiId"></param>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static BusinessInfoRes Get(string poiId, string tokenOrAppId = "")
        {
            var data = new { poi_id = poiId };
            var url = $"http://api.weixin.qq.com/cgi-bin/poi/getpoi?access_token={OpenApi.GetToken(tokenOrAppId)}";
            return HttpUtil.PostResult<BusinessInfoRes>(data, url);
        }
        /// <summary>
        /// 获取的门店列表
        /// </summary>
        /// <param name="begin">开始位置，0 即为从第一条开始查询</param>
        /// <param name="limit">返回数据条数，最大允许50，默认为20</param>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static PoisRes GetList(int begin = 0, int limit = 20, string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/poi/getpoilist?access_token={OpenApi.GetToken(tokenOrAppId)}";
            var data = new { begin = begin, limit = limit };
            return HttpUtil.PostResult<PoisRes>(data, url);
        }
        /// <summary>
        /// 修改门店服务信息。字段若有填写内容则为覆盖更新，若无内容则视为不修改，维持原有内容。 photo_list 字段为全列表覆盖，若需要增加图片，需将之前图片同样放入list 中，在其后增加新增图片。
        /// </summary>
        /// <param name="info"></param>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static BaseRes UpdatePoiServiceInfo(PoiServiceInfo info, string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/poi/updatepoi?access_token={OpenApi.GetToken(tokenOrAppId)}";
            var data = new
            {
                business = new
                {
                    base_info = info
                }
            };
            return HttpUtil.PostResult<BaseRes>(data, url);
        }
        /// <summary>
        /// 删除门店
        /// </summary>
        /// <param name="poiid"></param>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static BaseRes Delete(string poiid, string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/poi/delpoi?access_token={OpenApi.GetToken(tokenOrAppId)}";
            var data = new { poi_id = poiid };
            return HttpUtil.PostResult<BaseRes>(data, url);
        }
        /// <summary>
        /// 获取门店类目表
        /// </summary>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static PoiCategorys GetCategorys(string tokenOrAppId = "")
        {
            var url = $"http://api.weixin.qq.com/cgi-bin/poi/getwxcategory?access_token={OpenApi.GetToken(tokenOrAppId)}";
            return HttpUtil.GetResult<PoiCategorys>(url);
        }
#if !NET40
        public static async Task<ImgUrl> UploadImgAsync(string filename, string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={await OpenApi.GetTokenAsync(tokenOrAppId)}";
            var form = new List<FormEntity>
            {
                new FormEntity { IsFile = true, Name = "buffer", Value = filename }
            };
            return await HttpUtil.UploadResultAsync<ImgUrl>(url, form);
        }
        public static async Task<BaseRes> AddAsync(PoiInfo info, string tokenOrAppId = "")
        {
            var data = new BusinessInfo { business = new BusinessInfo.BaseInfo { base_info = info } };
            var url = $"http://api.weixin.qq.com/cgi-bin/poi/addpoi?access_token={await OpenApi.GetTokenAsync(tokenOrAppId)}";
            return await HttpUtil.PostResultAsync<BaseRes>(data, url);
        }
        /// <summary>
        /// 获取的门店列表
        /// </summary>
        /// <param name="begin">开始位置，0 即为从第一条开始查询</param>
        /// <param name="limit">返回数据条数，最大允许50，默认为20</param>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static async Task<PoisRes> GetListAsync(int begin = 0, int limit = 20, string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/poi/getpoilist?access_token={await OpenApi.GetTokenAsync(tokenOrAppId)}";
            var data = new { begin = begin, limit = limit };
            return await HttpUtil.PostResultAsync<PoisRes>(data, url);
        }
        /// <summary>
        /// 修改门店服务信息.字段若有填写内容则为覆盖更新，若无内容则视为不修改，维持原有内容。 photo_list 字段为全列表覆盖，若需要增加图片，需将之前图片同样放入list 中，在其后增加新增图片。
        /// </summary>
        /// <param name="info"></param>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static async Task<BaseRes> UpdatePoiServiceInfoAsync(PoiServiceInfo info, string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/poi/updatepoi?access_token={await OpenApi.GetTokenAsync(tokenOrAppId)}";
            var data = new
            {
                business = new
                {
                    base_info = info
                }
            };
            return await HttpUtil.PostResultAsync<BaseRes>(data, url);
        }

        /// <summary>
        /// 删除门店
        /// </summary>
        /// <param name="poiid"></param>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static async Task<BaseRes> DeleteAsync(string poiid, string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/poi/delpoi?access_token={await OpenApi.GetTokenAsync(tokenOrAppId)}";
            var data = new { poi_id = poiid };
            return await HttpUtil.PostResultAsync<BaseRes>(data, url);
        }
        /// <summary>
        /// 获取门店类目表
        /// </summary>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static async Task<PoiCategorys> GetCategorysAsync(string tokenOrAppId = "")
        {
            var url = $"http://api.weixin.qq.com/cgi-bin/poi/getwxcategory?access_token={await OpenApi.GetTokenAsync(tokenOrAppId)}";
            return await HttpUtil.GetResultAsync<PoiCategorys>(url);
        }
#endif
    }
}
